package org.zoomdev.zoom.common.config;

import org.zoomdev.zoom.common.io.Io;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class SimpleReader implements ConfigLoader {


    @Override
    public Map<String, Object> load(InputStream in) throws IOException {
        try {
            Properties p = new Properties();
            p.load(new InputStreamReader(in, StandardCharsets.UTF_8));
            Map<String, Object> map = new HashMap<>();
            Enumeration<Object> keys = p.keys();
            while (keys.hasMoreElements()) {
                String key = (String) keys.nextElement();
                String value = p.getProperty(key);
                map.put(key, value);
            }
            return map;
        } finally {
            Io.close(in);
        }
    }
}
